//
//  MNDGuideViewController.m
//  miniwangdai2
//
//  Created by 谭伟 on 2017/4/7.
//  Copyright © 2017年 谭谭伟. All rights reserved.
//

#import "MNDGuideViewController.h"
#import "MNDGuideStepView.h"
#import "MNDGuideTableViewCell.h"
#import "MNDGuideProblemModel.h"

#define kBottomLineWidth [UIScreen mainScreen].bounds.size.width*0.2
@interface MNDGuideViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;


//底部红线到屏幕左侧的间距
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leadMargin;

@property (weak, nonatomic) IBOutlet UIButton *firstButton;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *guideFlowLayout;
/* 选中的按钮 */
@property (strong, nonatomic) UIButton *selectButton;
/* 最后一页tabview */
@property (strong, nonatomic) UITableView *tabView;
/* MNDGuideProblemModel */
@property (strong, nonatomic) MNDGuideProblemModel *guideProblemModel;
@end

@implementation MNDGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _guideProblemModel = [[MNDGuideProblemModel alloc] init];
    _selectButton = _firstButton;
    _selectButton.selected = YES;
    
}
- (IBAction)guideProjectClick:(UIButton *)sender {
    
    _selectButton.selected = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.leadMargin.constant = (sender.tag-100)*kBottomLineWidth;
        self.collectionView.contentOffset = CGPointMake(kScreenWidth*(sender.tag-100), 0);
    }];
    sender.selected = YES;
    _selectButton = sender;
}


#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 5;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    collectionView.pagingEnabled = YES;
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuideCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor redColor];
  
    //设置cell
    _guideFlowLayout.minimumLineSpacing = 0;
    _guideFlowLayout.minimumInteritemSpacing = 0;
    _guideFlowLayout.itemSize = collectionView.bounds.size;
    _guideFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //搭建cell内部界面
    [self setupCellUIWithCell:cell andIndexPath:indexPath];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //让上面的button和button底部线条跟着滚动改变
    _selectButton.selected = NO;
    CGFloat number = (_collectionView.contentOffset.x)/kScreenWidth;
    self.leadMargin.constant = number*kBottomLineWidth;
    UIButton *button = [self.view viewWithTag:number+100];
    button.selected = YES;
    _selectButton = button;
    

}
#pragma mark -搭建cell内部界面

- (void)setupCellUIWithCell:(UICollectionViewCell*)cell andIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *guideViews =[[NSBundle mainBundle]loadNibNamed:@"MNDGuideStepView" owner:self options:nil];
    switch (indexPath.row) {
        case 0:
        {
            MNDGuideStepView *stepView = guideViews[0];
            [cell.contentView addSubview:stepView];
            [stepView mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.center.equalTo(cell);
                make.leading.trailing.top.bottom.mas_equalTo(cell.contentView);

            }];
            
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"只需四步轻松出借"];
            //设置字体颜色
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
            //字号
            [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(0, 8)];
            UILabel *topLabel = [[UILabel alloc]init];
            topLabel.attributedText = str;
            [cell.contentView addSubview:topLabel];
            [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(cell.contentView);
                make.top.equalTo(cell.mas_top).mas_offset(30);
            }];
        }
            break;
        case 1:
        {
            MNDGuideStepView *stepView = guideViews[1];
            [cell.contentView addSubview:stepView];
            [stepView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.top.bottom.mas_equalTo(cell.contentView);
                
            }];
        }
            
            break;
        case 2:
        {
            MNDGuideStepView *stepView = guideViews[2];
            [cell.contentView addSubview:stepView];
            [stepView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.top.bottom.mas_equalTo(cell.contentView);
                
            }];
        }
            
            break;
        case 3:
        {
            MNDGuideStepView *stepView = guideViews[3];
            [cell addSubview:stepView];
//            scroview.dir
            [stepView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.top.bottom.mas_equalTo(cell);
                
            }];
        }
            
            break;
        case 4:
        {

            _tabView = [[UITableView alloc]init];
            _tabView.allowsSelection = NO;
            _tabView.dataSource = self;
            _tabView.delegate = self;
            _tabView.showsVerticalScrollIndicator = NO;
            _tabView.showsHorizontalScrollIndicator = NO;
            [cell.contentView addSubview:_tabView];
            [_tabView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.top.bottom.mas_equalTo(cell.contentView);
                
            }];
            //自动计算行高
            _tabView.rowHeight = UITableViewAutomaticDimension;
            _tabView.estimatedRowHeight = 44;
            _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;

        }
            
            
            break;
            
        default:
            break;
    }

}


#pragma mark -UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    MNDGuideTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"guideCell"];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MNDGuideTableViewCell" owner:nil options:nil];
        cell = [nibs lastObject];
        cell.backgroundColor = [UIColor clearColor];
    };

    //设置label
    cell.detailLabel.text = _guideProblemModel.problemDescArray[indexPath.row];
    
    if (indexPath.row%2 == 1) {//奇数行
        cell.detailLabel.font = [UIFont systemFontOfSize:18];
        cell.detailLabel.textColor = [UIColor darkGrayColor];

    } else {//偶数行

        cell.detailLabel.font = [UIFont systemFontOfSize:20];
        cell.detailLabel.textColor = [UIColor blackColor];
        
    }
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
